package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.union.UnionConstructConfig;
import com.yanqu.road.entity.union.UnionUpgradeInfo;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionConfigDaoImpl extends TempDao {

    public List<UnionUpgradeInfo> getUnionUpgradeInfoList() {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UnionUpgradeInfo> dataList = new ArrayList<>();
        String sql = "select * from t_p_union_upgrade order by level";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionUpgradeInfo data = new UnionUpgradeInfo();
                data.setLevel(rs.getInt("level"));
                data.setExperience(rs.getLong("experience"));
                data.setMaxMemberCount(rs.getInt("maxPerson"));
                data.setDeputyMasterCount(rs.getInt("vicePresident"));
                data.setCreamCount(rs.getInt("elite"));
                data.setDiscount(rs.getInt("discount"));
                data.setQualityPro(rs.getString("qualityPro"));
                data.setNewMaxPerson(rs.getString("newMaxPerson"));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public Map<Integer, UnionConstructConfig> getUnionConstructConfigMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UnionConstructConfig> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_union_construct";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionConstructConfig data = new UnionConstructConfig();
                data.setType(rs.getInt("type"));
                data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
                data.setMaxTimes(rs.getInt("maxNum"));
                data.setAddProgress(rs.getInt("addProgress"));
                data.setAddUnionExp(rs.getInt("addGuildExp"));
                data.setAddUnionWealth(rs.getInt("addGuildWealth"));
                data.setAddContribution(rs.getInt("addPersonContribution"));
                data.setCoolTime(rs.getInt("coolingTime"));
                dataMap.put(data.getType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
